package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.CardMsg;

import java.util.ArrayList;

/**
 * @author Denis
 * @ClassName: CardTypeAdapter
 * @Description: 卡片类型适配器
 * @date 2017/8/25 15:57
 */

public class CardTypeAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<CardMsg.CardAllType> card_all_type;

    public CardTypeAdapter(Context mContext, ArrayList<CardMsg.CardAllType> card_all_type) {
        this.mContext = mContext;
        this.card_all_type = card_all_type;
    }

    public void setCard_all_type(ArrayList<CardMsg.CardAllType> card_all_type) {
        this.card_all_type = card_all_type;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return card_all_type == null ? 0 : card_all_type.size();
    }

    @Override
    public Object getItem(int position) {
        return card_all_type.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = new TextView(mContext);
        try {
            CardMsg.CardAllType item = card_all_type.get(position);
            textView.setText(item.type_name);
            textView.setTag(item.type_id);
            textView.setTextSize(16f);
            textView.setGravity(Gravity.CENTER);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(layoutParams);
            int bottom = position == getCount() - 1 ? 10 : 5;
            textView.setPadding(12, 10, 12, bottom);
            textView.setMinEms(4);
            textView.setBackgroundResource(R.drawable.while_gray_ripple);
            return textView;
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        return textView;
    }
}
